﻿// LoginDlg.cpp: 实现文件
//

#include "stdafx.h"
#include "LoginDlg.h"
#include "afxdialogex.h"
#include <direct.h>
#include <fstream>
#include "md5.h"
#include "CModifyPassword.h"
#include <map>
#include "resource.h"

std::map<std::string, std::string> m_UserInfos;
// LoginDlg 对话框

IMPLEMENT_DYNAMIC(CLoginDlg, CDialogEx)

CLoginDlg::CLoginDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_LOGIN, pParent), _workingDirectory(_getcwd(nullptr, 1000))
{
	
}

CLoginDlg::~CLoginDlg()
{
}

void CLoginDlg::CreateDefaultUserInfo(std::string path, std::string content )
{
	FILE* fp = nullptr;
	fopen_s(&fp, path.c_str(), "w+");
	fprintf(fp, content.c_str());
	fclose(fp);
}

void CLoginDlg::CreateDefaultUserInfo(bool isSetDefualtInfos)
{
	std::string path(_workingDirectory);
	path.append("\\info");
	if (!PathIsDirectory(path.c_str()))
		CreateDirectory(path.c_str(), nullptr);
	path.append("\\UserInfo.txt");
	FILE* fp = nullptr;
	fopen_s(&fp, path.c_str(), "w+");
	if (isSetDefualtInfos)
	{
		m_UserInfos.clear();
		m_UserInfos.insert(std::map<std::string, std::string>::value_type("Operator", ""));
		m_UserInfos.insert(std::map<std::string, std::string>::value_type("Engineer", MD5("Engineer").toStr()));
		m_UserInfos.insert(std::map<std::string, std::string>::value_type("Administrator", MD5("Administrator").toStr()));
	}

	std::string content("Operator,");
	content.append(m_UserInfos.at("Operator"));
	content.append("\n");
	content.append("Engineer,");
	content.append(m_UserInfos.at("Engineer"));
	content.append("\n");
	content.append("Administrator,");
	content.append(m_UserInfos.at("Administrator"));
	fprintf(fp, content.c_str());
	fclose(fp);
}

void CLoginDlg::Close()
{
	std::string path(_workingDirectory);
	path.append("\\info");
	if (!PathIsDirectory(path.c_str()))
		CreateDirectory(path.c_str(), nullptr);
	path.append("\\UserLoginInfo.txt");
	CString content;
	m_cbUserList.GetWindowTextA(content);
	CreateDefaultUserInfo(path, content.GetBuffer());
	switch (m_cbUserList.GetCurSel())
	{
	case 0:
		EndDialog(OPERATOR);
		break;
	case 1:
		EndDialog(ENGINEER);
		break;
	case 2:
		EndDialog(ADMINISTRATOR);
		break;
	default:
		EndDialog(OPERATOR);
		break;
	}
}

void CLoginDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_USER_LIST, m_cbUserList);
	DDX_Control(pDX, IDC_PASSWORD, m_txtPassword);
}


BEGIN_MESSAGE_MAP(CLoginDlg, CDialogEx)
	ON_BN_CLICKED(IDC_EXIT, &CLoginDlg::OnBnClickedExit)
	ON_BN_CLICKED(IDC_CONFIRM, &CLoginDlg::OnBnClickedComfirm)
	ON_BN_CLICKED(IDC_MODIFY, &CLoginDlg::OnBnClickedModify)
END_MESSAGE_MAP()


// LoginDlg 消息处理程序


void CLoginDlg::OnBnClickedExit()
{
	// TODO: 在此添加控件通知处理程序代码
	EndDialog(IDCANCEL);
}


void CLoginDlg::OnBnClickedComfirm()
{
	// TODO: 在此添加控件通知处理程序代码
	//std::string path(_workingDirectory);
	//path.append("\\info");
	//if (!PathIsDirectory(path.c_str()))
	//	CreateDirectory(path.c_str(), nullptr);
	//path.append("\\UserInfo.txt");
	//std::ifstream fin(path, std::ios::in);
	//if (!fin.good())//如果文件不存在,添加默认值
	//{
	//	CreateDefaultUserInfo(path);
	//	fin = std::ifstream(path, std::ios::in);
	//}
	std::string temp;
	CString userName;
	CString userPassword;//input password
	m_cbUserList.GetWindowTextA(userName);
	m_txtPassword.GetWindowTextA(userPassword);
	if(userName.GetLength() == 0)
		MessageBox(_T("User name cannot be empty!"), "Error", MB_ICONERROR);
	std::string _userName(userName.GetBuffer(0));
	if ((userPassword.GetLength() == 0 && !m_UserInfos.at(_userName).empty())
		|| (userPassword.GetLength() != 0 && MD5(userPassword.GetBuffer(0)).toStr() != m_UserInfos.at(_userName)))
	{
		MessageBox(_T("Password error!"), "Error", MB_ICONERROR);
		return;
	}
	Close();
}


BOOL CLoginDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	m_hIcon = AfxGetApp()->LoadIcon(IDI_CYG_LOGO);
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	m_cbUserList.SetCurSel(0);

	std::string path(_workingDirectory);
	path.append("\\info\\UserInfo.txt");
	std::ifstream fin(path, std::ios::in);
	m_UserInfos.clear();
	m_UserInfos.insert(std::map<std::string, std::string>::value_type("Operator", ""));
	m_UserInfos.insert(std::map<std::string, std::string>::value_type("Engineer", MD5("Engineer").toStr()));
	m_UserInfos.insert(std::map<std::string, std::string>::value_type("Administrator", MD5("Administrator").toStr()));
	if (fin.good())//如果文件存在,则修改值
	{
		std::string temp;
		while (std::getline(fin, temp))
		{
			size_t index = temp.find(',');
			if(index == -1)
			{
				fin.close();
				MessageBox(_T("Checked that the configuration file is not in the correct format and has been reset to the default configuration!"), "Info", MB_ICONINFORMATION);
				CreateDefaultUserInfo();
				break;
			}
			string name = temp.substr(0, index);
			string password = temp.substr(index + 1);
			if (m_UserInfos.find(name) != m_UserInfos.end())
				m_UserInfos.at(name) = password;
			else
			{
				fin.close();
				MessageBox(_T("Check that the user name in the configuration file has been changed and has been reset to the default configuration!"), "Info", MB_ICONINFORMATION);
				CreateDefaultUserInfo();
				break;
			}
		}
	}
	else
	{
		CreateDefaultUserInfo();
	}

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CLoginDlg::OnBnClickedModify()
{
	// TODO: 在此添加控件通知处理程序代码
	CModifyPassword dlg;
	ShowWindow(SW_HIDE);
	if (dlg.DoModal() == IDOK)//更新密码字典
	{
		std::string path(_workingDirectory);
		path.append("\\info\\UserInfo.txt");
		std::ifstream fin(path, std::ios::in);
		if (fin.good())//如果文件存在,则修改值
		{
			std::string temp;
			while (std::getline(fin, temp))
			{
				size_t index = temp.find(',');
				if (index == -1)
					break;
				string name = temp.substr(0, index);
				string password = temp.substr(index + 1);
				if (m_UserInfos.find(name) != m_UserInfos.end())
					m_UserInfos.at(name) = password;
			}
		}
	}
	ShowWindow(SW_SHOW);
}


BOOL CLoginDlg::PreTranslateMessage(MSG* pMsg)
{
	// TODO: 在此添加专用代码和/或调用基类
	if (pMsg->message == WM_KEYDOWN)
	{
		switch (pMsg->wParam)
		{
		case VK_RETURN:
			OnBnClickedComfirm();
			return true;
		default:
			break;
		}
	}
	return CDialogEx::PreTranslateMessage(pMsg);
}
